package com.xpf.controller;

import com.xpf.pojo.Items;
import com.xpf.pojo.ItemsImg;
import com.xpf.pojo.ItemsParam;
import com.xpf.pojo.ItemsSpec;
import com.xpf.pojo.vo.CommentLevelCountsVO;
import com.xpf.pojo.vo.ItemInfoVO;
import com.xpf.pojo.vo.ShopcartVO;
import com.xpf.service.ItemService;
import com.xpf.utils.JSONResult;
import com.xpf.utils.PagedGridResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.models.auth.In;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(value = "商品接口", tags = {"商品信息展示的相关接口"})
@RestController //所有请求都是json对象
@RequestMapping("items")
public class ItemsController extends BaseController{

    @Autowired
    private ItemService itemService;

    @ApiOperation(value = "查询商品详情", notes = "查询商品详情)", httpMethod = "GET")
    @GetMapping("/info/{itemId}")
    public JSONResult sixNewItems(
            @ApiParam(name = "itemId", value = "商品id", required = true)
            @PathVariable String itemId){

        if (StringUtils.isBlank(itemId)){
            return JSONResult.errorMsg(null);
        }

        Items items = itemService.queryItemById(itemId);
        List<ItemsImg> itemsImgList = itemService.queryItemImgList(itemId);
        List<ItemsSpec> itemsSpecList = itemService.queryItemsSpec(itemId);
        ItemsParam itemsParam = itemService.queryItemsParam(itemId);

        ItemInfoVO itemInfoVO = new ItemInfoVO();
        itemInfoVO.setItem(items);
        itemInfoVO.setItemImgList(itemsImgList);
        itemInfoVO.setItemSpecList(itemsSpecList);
        itemInfoVO.setItemParams(itemsParam);

        return JSONResult.ok(itemInfoVO);
    }

    @ApiOperation(value = "查询商品三种评价的数量", notes = "查询商品三种评价的数量)", httpMethod = "GET")
    @GetMapping("/commentLevel")
    public JSONResult commentLevel(
            @ApiParam(name = "itemId", value = "商品id", required = true)
            @RequestParam String itemId){

        if (StringUtils.isBlank(itemId)){
            return JSONResult.errorMsg(null);
        }
        CommentLevelCountsVO countsVO = itemService.queryCommentCounts(itemId);

        return JSONResult.ok(countsVO);
    }

    @ApiOperation(value = "查询商品评论（分页）", notes = "查询商品评论（分页）)", httpMethod = "GET")
    @GetMapping("/comments")
    public JSONResult comments(
            @ApiParam(name = "itemId", value = "商品id", required = true)
            @RequestParam String itemId,
            @ApiParam(name = "level", value = "评价等级", required = false)
            @RequestParam Integer level,
            @ApiParam(name = "page", value = "当前第几页", required = false)
            @RequestParam Integer page,
            @ApiParam(name = "pageSize", value = "分页的每一页的页数", required = false)
            @RequestParam Integer pageSize
            ){

        if (StringUtils.isBlank(itemId)){
            return JSONResult.errorMsg(null);
        }
        if (page == null)
            page = 1;
        if (pageSize == null)
            pageSize = COMMON_PAGE_SIZE;

        PagedGridResult gridResult = itemService.queryPagedComments(itemId, level, page, pageSize);
        return JSONResult.ok(gridResult);
    }

    @ApiOperation(value = "搜索商品列表（分页）", notes = "搜索商品列表（分页）", httpMethod = "GET")
    @GetMapping("/search")
    public JSONResult search(
            @ApiParam(name = "keywords", value = "关键字", required = true)
            @RequestParam String keywords,
            @ApiParam(name = "sort", value = "排序。k:默认，根据name排序;c根据价格;p根据销量", required = false)
            @RequestParam String sort,
            @ApiParam(name = "page", value = "当前第几页", required = false)
            @RequestParam Integer page,
            @ApiParam(name = "pageSize", value = "分页的每一页的页数", required = false)
            @RequestParam Integer pageSize
    ){

        if (StringUtils.isBlank(keywords)){
            return JSONResult.errorMsg(null);
        }
        if (page == null)
            page = 1;
        if (pageSize == null)
            pageSize = PAGE_SIZE;

        PagedGridResult gridResult = itemService.searchItems(keywords, sort, page, pageSize);
        return JSONResult.ok(gridResult);
    }

    @ApiOperation(value = "根据三级分类搜索其分类下的所有商品（分页）", notes = "根据三级分类搜索其分类下的所有商品（分页）", httpMethod = "GET")
    @GetMapping("/catItems")
    public JSONResult catItems(
            @ApiParam(name = "catId", value = "三级分类id", required = true)
            @RequestParam Integer catId,
            @ApiParam(name = "sort", value = "排序。k:默认，根据name排序;c根据价格;p根据销量", required = false)
            @RequestParam String sort,
            @ApiParam(name = "page", value = "当前第几页", required = false)
            @RequestParam Integer page,
            @ApiParam(name = "pageSize", value = "分页的每一页的页数", required = false)
            @RequestParam Integer pageSize
    ){

        if (catId == null){
            return JSONResult.errorMsg(null);
        }
        if (page == null)
            page = 1;
        if (pageSize == null)
            pageSize = PAGE_SIZE;

        PagedGridResult gridResult = itemService.searchItemsByThirdCat(catId, sort, page, pageSize);
        return JSONResult.ok(gridResult);
    }

    @ApiOperation(value = "根据商品规格ids查找最新的商品数据", notes = "根据商品规格ids查找最新的商品数据", httpMethod = "GET")
    @GetMapping("/refresh")
    public JSONResult refresh(
            @ApiParam(name = "itemSpecIds", value = "拼接的规格ids", required = true, example = "1001,1003,1005")
            @RequestParam String itemSpecIds
    ){
        if (StringUtils.isBlank(itemSpecIds))
            return JSONResult.ok();


        List<ShopcartVO> list = itemService.queryItemsBySpecIds(itemSpecIds);
        return JSONResult.ok(list);
    }

}
